using System.Collections;
using DG.Tweening;
using Dream.ScenesLoader.SceneData;
using Light;
using Light.Assets.Scripts.EventManager;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Dream.ScenesLoader
{
    //sky 24-11-25 04:12:18 
    public class SceneLoader : MonoBehaviour
    {
        [SerializeField] private GameSceneSO firstLoadScene; //游戏开始需要加载的场景

        //淡入淡出
        public float fadeDuration;
        public Image fadeImage;
        public Vector2 targetSize;
        private GameSceneSO currentLoadScene; //加载新场景时需要卸载的场景
        private GameObject imageTalk;

        private bool isFadeScene;
        private GameSceneSO needLoadScene; //需要加载的新场景

        private void Awake()
        {
            EventManager.Register<SceneLoadEvent>(LoadScene);
            EventManager.Register<SceneLoadEvent>(ExitCurrentScene);
            EventManager.Register<LoadFirstSceneEvent>(OnLoadFirstSceneEventHandle);
            imageTalk = fadeImage.transform.Find("Text").gameObject;
        }

        private void OnDestroy()
        {
            EventManager.Unregister<SceneLoadEvent>(LoadScene);
            EventManager.Unregister<SceneLoadEvent>(ExitCurrentScene);
            EventManager.Unregister<LoadFirstSceneEvent>(OnLoadFirstSceneEventHandle);
        }

        /// <summary>
        ///     接收指挥，手动加载
        /// </summary>
        /// <param name="obj"></param>
        private void OnLoadFirstSceneEventHandle(LoadFirstSceneEvent obj)
        {
            currentLoadScene = firstLoadScene;
            currentLoadScene.sceneReference.LoadSceneAsync(LoadSceneMode.Additive);
        }

        private void LoadScene(SceneLoadEvent evt)
        {
            if (evt.isExitScene)
                return;
            needLoadScene = evt.sceneData;
            isFadeScene = evt.isFadeScreen;
            if (currentLoadScene != null)
                StartCoroutine(UnLoadPreviousScene());
        }

        private void ExitCurrentScene(SceneLoadEvent evt)
        {
            if (!evt.isExitScene)
                return;
            needLoadScene = firstLoadScene;
            isFadeScene = true;
            if (currentLoadScene != null)
                StartCoroutine(UnLoadSceneExit());
        }

        //进入梦境
        private IEnumerator UnLoadPreviousScene()
        {
            fadeImage.transform.parent.gameObject.SetActive(true);
            fadeImage.DOColor(new Color(fadeImage.color.r, fadeImage.color.g, fadeImage.color.b, 1), fadeDuration)
                .OnComplete(
                    () => { imageTalk.SetActive(true); }
                );
            yield return new WaitForSeconds(1);
            yield return new WaitUntil(() => LightInputManager.Instance.ContinueTalkInput);
            imageTalk.SetActive(false);

            if (isFadeScene)
                //切换场景时的切换效果
                fadeImage.rectTransform.DOSizeDelta(targetSize, fadeDuration)
                    .SetEase(Ease.OutQuad); // 放大到目标大小，并将其加入到移动的动画一起进行

            yield return new WaitForSeconds(fadeDuration);
            yield return currentLoadScene.sceneReference.UnLoadScene();

            if (isFadeScene)
                fadeImage.DOColor(new Color(fadeImage.color.r, fadeImage.color.g, fadeImage.color.b, 0), fadeDuration)
                    .OnComplete(
                        () => { fadeImage.transform.parent.gameObject.SetActive(false); });

            LoadNewScene();
        }

        //退出梦境
        private IEnumerator UnLoadSceneExit()
        {
            if (isFadeScene)
            {
                fadeImage.transform.parent.gameObject.SetActive(true);
                fadeImage.DOColor(new Color(fadeImage.color.r, fadeImage.color.g, fadeImage.color.b, 1), fadeDuration);
            }

            yield return new WaitForSeconds(fadeDuration);
            yield return currentLoadScene.sceneReference.UnLoadScene();

            LoadNewScene();

            if (isFadeScene)
            {
                //切换场景时的切换效果
                fadeImage.rectTransform.DOSizeDelta(new Vector2(450, 200), fadeDuration)
                    .SetEase(Ease.OutQuad); // 放大到目标大小，并将其加入到移动的动画一起进行
                yield return new WaitForSeconds(fadeDuration);
                fadeImage.transform.parent.gameObject.SetActive(false);
                fadeImage.color = new Color(fadeImage.color.r, fadeImage.color.g, fadeImage.color.b, 0);
            }

            //PlayAwakeAnim();
        }

        private void PlayAwakeAnim()
        {
            using var evt = IsAwake.Get();
            evt.isSleepOrAwake = false;
            EventManager.SendEvent(evt);
        }

        private void LoadNewScene()
        {
            currentLoadScene = needLoadScene;
            needLoadScene.sceneReference.LoadSceneAsync(LoadSceneMode.Additive);
        }
    }
}